﻿#ifndef NAVIGATIONBAR_H
#define NAVIGATIONBAR_H

#include "../widget/stdafx.h"
#include "../widget/icon_helper.h"

#include <QPainter>
#include <QColor>
#include <QWidget>
#include <QPushButton>
#include <QPixmap>
#include <QPropertyAnimation>

class NavigationPushButton : public QPushButton {
    Q_OBJECT
    Q_PROPERTY_CREATE_NANO(QPixmap, selectPix)
    Q_PROPERTY_CREATE_NANO(QPixmap, unSelectPix)
    Q_PROPERTY_CREATE_NANO(QPixmap, unSelectFloatingPix)
    Q_PROPERTY_CREATE_NANO(bool, isHovered)
    Q_PROPERTY_CREATE_NANO(bool, isSelected)

public:
    explicit NavigationPushButton(QWidget *parent = nullptr):QPushButton(parent) {
        this->setisHovered(false);
        this->setisSelected(false);
    }

protected:
    virtual void paintEvent(QPaintEvent *event)override{
        QPainter painter(this);
        painter.setRenderHint(QPainter::Antialiasing, true);

        QString textstr = this->text();

        if(m_isSelected) {
            painter.save();
            painter.setPen(Qt::NoPen);
            painter.setBrush(QColor("#EDFEFF"));
            painter.drawRoundedRect(rect(), 10, 10);
            painter.restore();

            painter.save();
            painter.setPen(Qt::NoPen);
            painter.drawPixmap(rect().x()+10, rect().y()+7 , m_selectPix);
            painter.restore();

            painter.save();
            QFont textfont = painter.font();
            textfont.setBold(true);
            textfont.setPointSize(13);
            painter.setPen(QColor("#2CC3D4"));
            painter.setFont(textfont);
            painter.drawText(rect().x()+10+36, rect().y()+25 , textstr);
            painter.restore();
        } else {
            if(m_isHovered) {
                painter.save();
                painter.setPen(Qt::NoPen);
                painter.drawPixmap(rect().x()+8, rect().y()+5 , m_unSelectFloatingPix);
                painter.restore();

                painter.save();
                QFont textfont = painter.font();
                textfont.setBold(true);
                textfont.setPointSize(15);
                painter.setPen(QColor("#303133"));
                painter.setFont(textfont);
                painter.drawText(rect().x()+10+35, rect().y()+25 , textstr);
                painter.restore();
            } else {
                painter.save();
                painter.setPen(Qt::NoPen);
                painter.drawPixmap(rect().x()+10, rect().y()+7 , m_unSelectPix);
                painter.restore();

                painter.save();
                QFont textfont = painter.font();
                textfont.setBold(true);
                textfont.setPointSize(13);
                painter.setPen(QColor("#303133"));
                painter.setFont(textfont);
                painter.drawText(rect().x()+10+36, rect().y()+25 , textstr);
                painter.restore();
            }
        }
    }

    virtual void enterEvent(QEnterEvent *event)override {
        m_isHovered = true;
    }

    void leaveEvent(QEvent *event) override {
        m_isHovered = false;
    }
};

class NavigationBar : public QWidget
{
    Q_OBJECT
    Q_PROPERTY_CREATE_NANO(QString , currSelectName)
public:
    explicit NavigationBar(QWidget *parent = nullptr);

    void addPage(QWidget* page, QPixmap sepix, QPixmap unsepix,
                 QPixmap unsefloatingpix, QString text, bool defaultSe = false);

    void startRun();

public slots:
    void onCurrSelectBtnChange();

signals:
    void selectPageChange(QString name, QWidget* page);

protected:
    virtual void paintEvent(QPaintEvent *event)override;

private:
    QMap<QString, NavigationPushButton*> m_buttons;
    QMap<QString, QWidget*> m_pages;
    class QVBoxLayout* m_mainLayout = nullptr;
};

#endif // NAVIGATIONBAR_H
